package com.quwan.common;

import com.quwan.common.result.ApiResult;
import com.quwan.common.result.OssResultDTO;
import com.quwan.common.service.ContentSecurityService;
import com.quwan.common.service.FileUploadService;
import com.quwan.common.service.OssController;

import com.quwan.utils.ContentSecurityUtil;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;

/**
 * @author quan
 * @date 2021-07-23 17:53
 */
@Api(tags = "公用接口类")
@RestController
@RequestMapping("common")
public class CommonController {
    private final static Logger logger = LoggerFactory.getLogger(CommonController.class);
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    WebApplicationContext applicationContext;
    @Autowired
    private ContentSecurityService contentSecurityService;



    @ApiOperation(value = "upload（文件上传接口）", notes = "文件上传接口 " +
            "<br/>请求参数：file 选择文件上传 " +
            "<br/>请求参数：type: (image图片，video视频,default 其他) " +
            "<br/>返回参数： \"image/public/322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>" +
            "")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = ApiResult.class)
    })
    @PostMapping("upload/img")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "文件", required = true,dataTypeClass = MultipartFile.class)
    })
    @ResponseBody
    public ApiResult uploadImg(HttpServletRequest request, MultipartFile file) throws Exception {
        ApiResult result = contentSecurityService.checkImage(request);
        if (result.getCode() != 200) {
            return result;
        }
        String type = request.getParameter("type");
        logger.info("开始调用文件上传接口：type=" + type);
        List<OssResultDTO> ossResultDTOList = fileUploadService.uploadFileOss(request, type);
        logger.info("result:" + ossResultDTOList);
        return ApiResult.success(ossResultDTOList.get(0).getRelativePath());
    }

    @ApiOperation(value = "uploads（多文件上传接口）", notes = "文件上传接口 " +
            "<br/>请求参数：file 选择文件上传 " +
            "<br/>请求参数：type: (image图片，video视频,default 其他) " +
            "<br/>返回参数： " +
            "<br/>{\n" +
            "<br/> \"code\": \"0\",\n" +
            "<br/> \"msg\": \"操作成功\",\n" +
            "<br/> \"data\": [\n" +
            "<br/>     {\n" +
            "<br/>         \"fieldName\": \"任意\",\n" +
            "<br/>         \"originalName\": \"微信图片_20180817153046.jpg\",\n" +
            "<br/>         \"newName\": \"322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"relativePath\": \"image/public/322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"absPath\": \"http://ybya.oss-cn-hangzhou.aliyuncs.com/image/public/322c4e3e-dc89-43c2-85e2-ac4643644eff20180820.jpg\",\n" +
            "<br/>         \"suffix\": \".jpg\"\n" +
            "<br/>     }\n" +
            "<br/> ]\n" +
            "} " +
            "<br/>" +
            "")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = ApiResult.class)
    })
    @PostMapping("upload/imgs")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "文件", required = true,dataTypeClass = MultipartFile.class)
    })
    @ResponseBody
    public ApiResult uploadImgs(HttpServletRequest request, MultipartFile file) throws Exception {
        ApiResult result = contentSecurityService.checkImage(request);
        if (result.getCode() != 200) {
            return result;
        }
        String type = request.getParameter("type");
        logger.info("开始调用文件上传接口：type=" + type);
        List<OssResultDTO> ossResultDTOList = fileUploadService.uploadFileOss(request, type);
        logger.info("result:" + ossResultDTOList);
        return ApiResult.success(ossResultDTOList);
    }

    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "success", response = ApiResult.class),
            @ApiResponse(code = 400, message = "fail", response = ApiResult.class)
    })
    @ApiOperation(value = "文本内容检测")
    @PostMapping("content/check")
    public ApiResult checkContent(@ApiParam("文本内容") @RequestParam(required = true) String content)  {
        logger.info("开始调用文本检测接口：content=" + content);
        return contentSecurityService.checkContent(content);
    }

    public Function<Integer, Integer> sum() {
        return integer -> integer * 5;

    }

}
